package com.ruoyi.rocky.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.rocky.domain.PcProduct;
import com.ruoyi.rocky.service.PcProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品Controller
 * @date 2024-04-25
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/pcProduct")
@AllArgsConstructor
@Api(tags = "产品管理")
public class PcProductController extends BaseController {
    @Autowired
    private PcProductService pcProductService;

    /**
     * 查询产品列表
     */
    @ApiOperation(value = "查询产品列表")
    @GetMapping("/list")
    public TableDataInfo list(PcProduct pcProduct) {
        startPage();
        List<PcProduct> list = pcProductService.selectPcProductList(pcProduct);
        return getDataTable(list);
    }

//    /**
//     * 导出产品列表
//     */
//    @ApiOperation(value = "导出产品列表")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, PcProduct pcProduct) {
//        List<PcProduct> list = pcProductService.selectPcProductList(pcProduct);
//        ExcelUtil<PcProduct> util = new ExcelUtil<PcProduct>(PcProduct.class);
//        util.exportExcel(response, list, "产品数据");
//    }

    /**
     * 根据id查询产品
     */
    @ApiOperation(value = "根据产品id查询产品")
    @GetMapping(value = "/select/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        return R.ok(pcProductService.selectPcProductById(id));
    }

    /**
     * 新增产品
     */
    @ApiOperation(value = "新增产品")
    @PostMapping("/add")
    public R add(@RequestBody PcProduct pcProduct) {
        return R.ok(pcProductService.insertPcProduct(pcProduct));
    }

    /**
     * 修改产品
     */
    @ApiOperation(value = "修改产品")
    @PutMapping("/edit")
    public R edit(@RequestBody PcProduct pcProduct) {
        return R.ok(pcProductService.updatePcProduct(pcProduct));
    }

    /**
     * 上架商品
     * @param pcProduct
     * @return
     */
    @ApiOperation("上架商品")
    @PostMapping("/online")
    public R onlineProduct(@RequestBody PcProduct pcProduct) {
        return pcProductService.onlineProduct(pcProduct);
    }

    /**
     * 下架商品
     * @param pcProduct
     * @return
     */
    @ApiOperation("下架商品")
    @PostMapping("/offline")
    public R offlineProduct(@RequestBody PcProduct pcProduct) {
        return pcProductService.offlineProduct(pcProduct);
    }

    /**
     * 删除产品
     */
    @ApiOperation(value = "逻辑删除产品")
    @DeleteMapping("/delete/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return R.ok(pcProductService.deletePcProductByIds(ids));
    }
}

